package com.akkainaction.helloactor.actorselection;

import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.dispatch.OnFailure;
import akka.dispatch.OnSuccess;
import akka.util.Timeout;
import com.akkainaction.helloactor.actorselection.actor.TargetActor;
import scala.concurrent.Future;

import java.util.concurrent.TimeUnit;

public class TestActorSelectionResolveOne {

    public static void main(String[] args) {
        ActorSystem system = ActorSystem.create("sys");
        ActorRef targetActor = system.actorOf(Props.create(TargetActor.class), "targetActor");
        ActorSelection actorSelection = system.actorSelection("/user/targetActor");
        Timeout timeout = new Timeout(10, TimeUnit.SECONDS);
        Future<ActorRef> actorRefFuture = actorSelection.resolveOne(timeout);
        //成功查找到Actor
        actorRefFuture.onSuccess(new OnSuccess<ActorRef>() {
            @Override
            public void onSuccess(ActorRef actorRef) {
                System.out.println(actorRef);
            }
        },system.dispatcher());
        //查找失败时
        actorRefFuture.onFailure(new OnFailure() {
            @Override
            public void onFailure(Throwable throwable) {
                System.out.println(throwable);
            }
        },system.dispatcher());
    }
}
